package com.wu.server.service;

import com.wu.server.code.Code;
import com.wu.server.config.EncodeJson;
import com.wu.server.dao.GoodsRepository;
import com.wu.server.dao.ShopRepository;
import com.wu.server.entity.*;
import com.wu.server.websocket.ShopTopWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.websocket.EncodeException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 热销店铺TOP10
 * @author Wugs
 * @version 1.0
 * @date 2021/3/4 22:13
 */
@Component
public class ShopTopServer {
    @Autowired
    GoodsRepository goodsRepository;
    @Autowired
    ShopRepository shopRepository;
    @Autowired
    EncodeJson encodeJson;
    @Autowired
    Code code;

    /**
     * 外部调用接口
     * // TODO: 2021/3/6 先获取店铺名称，然后根据店铺名称查询对应的店铺销量sales，最后封装成hashMap返回
     */
    @Async
    public void execute(){
        shopRepository.deleteAll();
        Iterable<String> shopList = goodsRepository.getShopNameList();// TODO: 2021/3/6 获取店铺名称list
        saveShop(shopList);
        this.sendData();
    }
    public void sendData(){
        List<Object> data = new ArrayList<>();
        HashMap<String,Object> result = new HashMap<>();
        Iterable<Shop> shop_list = shopRepository.getShopList();
        int summary = getShopSum(shop_list);
        for(Shop shop : shop_list){
            HashMap<String,Object> new_data= new HashMap<>();
            new_data.put("name",shop.getShop_name());
            new_data.put("value",((float) shop.getShop_sales()/(float) summary));
            data.add(new_data);
        }
        result.put("data",data);
        HashMap<String,Object> meta = new HashMap<>();
        meta.put("code", code.SHOPTOP);
        meta.put("msg",code.getMsg(code.SHOPTOP));
        result.put("meta",meta);
        try {
            ShopTopWebSocket.sendMessage(encodeJson.encode(result));
        } catch (EncodeException e) {
            e.printStackTrace();
        }
    }
    private Integer getShopSum(Iterable<Shop> shop_list ){
        int sum = 0;
        for(Shop shop : shop_list){
            sum+=shop.getShop_sales();
        }
        System.out.println(sum);
        return sum;
    }
    /**
     * 存取店铺信息到shop表
     * @param shop_list
     */
    private void saveShop(Iterable<String> shop_list){
        int sales = 0;
        int id = 0;
        for(String shop_name : shop_list){// TODO: 2021/3/6 获取店铺名称
            id++;
            Shop shop = new Shop();
            Iterable<Goods> goods_list = goodsRepository.getShop(shop_name);// TODO: 2021/3/6 获取店铺商品信息用于计算sales
            shop.setId(id);
            shop.setShop_name(shop_name);
            shop.setShop_sales(saveSales(goods_list));//// TODO: 2021/3/6 获取店铺销量
            shopRepository.save(shop);// TODO: 2021/3/6 存取店铺信息到shop表
        }
        id = 0;


    }

    /**
     * 捕获单个店铺名称对应的商品销量
     * @param goods_list
     * @return
     */
    private int saveSales(Iterable<Goods> goods_list){
        int sales = 0;
        for(Goods goods : goods_list){
            sales += goods.getSales();
        }
        return sales;
    }
}
